<!--测评_评价等次信息表-->
<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" @close="clearDataList()" width="800px">
    <el-container>
      <el-main style="max-height:60vh">
        <el-form :model="dataFormNew"  ref="dataFormNew" @keyup.enter.native="dataFormSubmit()" label-width="140px">
          <el-row>
              <el-form-item label="等次名称" prop="djName" :rules="[
              { required: true, message: '等次名称不能为空', trigger: 'blur' },
              { min:1, max:100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
              ]">
                <el-input v-model="dataFormNew.djName" placeholder="等次名称" style="width: 90%"></el-input>
              </el-form-item>
              <el-form-item label="应用范围" prop="yyFw" :rules="[
                { required: true, message: '应用范围不能为空', trigger: 'blur' }
              ]">
                <el-select v-model="dataFormNew.yyFw" style="width: 90%">
                  <el-option v-for="(item,index) in ['个性','通用']" :key="index" :label="item" :value="item">
                  </el-option>
                </el-select>
              </el-form-item>
          </el-row>
<!--          <el-row v-for="(item,index) in dataForm.dataList" :key="index">-->

<!--            <el-form-item label="等次名称" :prop="'dataList['+index+'].djName'" :rules="[-->
<!--            { required: true, message: '规则名称不能为空', trigger: 'blur' },-->
<!--            { min:1, max:30, message: '长度在 1 到 30 个字符', trigger: 'blur' }-->
<!--            ]">-->

<!--              <el-input v-model="item.djName" placeholder="等次名称"></el-input>-->
<!--            </el-form-item>-->

<!--            <el-form-item label="应用范围" :prop="'dataList['+index+'].yyFw'" :rules="[-->
<!--              { required: true, message: '应用范围不能为空', trigger: 'blur' }-->
<!--            ]">-->
<!--              <el-select v-model="item.yyFw">-->
<!--                <el-option v-for="(item,index) in ['私有','公用']" :key="index" :label="item" :value="item">-->
<!--                </el-option>-->
<!--              </el-select>-->

<!--              <el-button @click="addItem()" v-if="index==dataForm.dataList.length-1" style="float: right" icon="el-icon-plus">-->
<!--              </el-button>-->
<!--              <el-button @click="removeItem(index)" v-if="index<dataForm.dataList.length-1" style="float: right" icon="el-icon-minus">-->
<!--              </el-button>-->
<!--            </el-form-item>-->
<!--          </el-row>-->
        </el-form>
      </el-main>
    </el-container>
    <span slot="footer" class="dialog-footer">

      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
      <el-button type="primary" plain @click="visible = false">取消</el-button>
    </span>

  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj } from '@/api/ceping/cepingdjxxb'

export default {
  data() {
    return {
      visible: false,
      canSubmit: false,
      editTab: false,

      dataFormNew:{
        djName: '',
        yyFw: ''
      },
      dataForm: {
        dataList: [{
          djName: '',
          yyFw: ''
        }],
      }

    }
  },
  methods: {
    //初始化赋值
    setInitCpDj(){
      // this.dataForm = {dataList: [{ djName: '',yyFw: ''}],
      this.dataForm = [];
      this.dataFormNew ={
        djName: '',
          yyFw: ''
      }

    },
    init(id) {
      //this.dataForm.id = id || 0;
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        //this.$refs['dataForm'].resetFields();
        this.$refs['dataFormNew'].resetFields();


        this.setInitCpDj();
        if (id) {
          getObj(id).then(response => {
            this.dataFormNew = response.data.data

           //console.log(this.dataFormNew );

            this.editTab = true;
          })
        }
        // if (id) {
        //   this.dataForm.dataList = [{
        //     djName: '',
        //     yyFw: ''
        //   }];
        //   getObj(id).then(response => {
        //     this.dataForm.dataList.push(response.data.data);
        //     this.editTab = true;
        //   });
        // }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataFormNew'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.editTab) {
            ////console.log('编辑');
            ////console.log(this.dataFormNew)

            this.dataForm.dataList = [];
            this.dataForm.dataList.push(this.dataFormNew);

            putObj(this.dataForm.dataList).then(data => {
              this.$notify.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            ////console.log('新增');
            ////console.log(this.dataFormNew)

            this.dataForm.dataList = [];
            this.dataForm.dataList.push(this.dataFormNew);

            addObj(this.dataForm.dataList).then(data => {
              this.$notify.success('添加成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    }
    ,
    //添加数据项
    addItem() {
      this.dataForm.dataList.push({
        djName: '',
        yyFw: ''
      });
    },
    //移除数据项
    removeItem(index) {
      this.dataForm.dataList.splice(index, 1);
    },
    clearDataList() {
      this.editTab = false;
      this.dataFormNew ={
        djName: '',
        yyFw: ''
      };
      this.dataForm.dataList = [];
      // this.dataForm.dataList.push({
      //   djName: '',
      //   yyFw: ''
      // });
    }
  }
}
</script>
<style scoped>
</style>
